<template>
  <div class="edit">
    <h2>编辑个人资料</h2>
    <form action="" enctype="multipart/form-data">
      <!-- 用户名 -->
      <div class="username">
        <label for="username">用户名</label>
        <input type="text" id="username" :value="username" placeholder="用户名">
      </div>
      <!-- 头像 上传后显示预览头像 -->
      <div class="avatar">
        <div class="imgBox">
          <img v-if="avatarUrl" :src="avatarUrl" alt="">
          <img v-else src="~assets/img/update.png" alt="">
        </div>
        <input type="file" id="file" accept="image/png, image/jpeg, image/gif, image/jpg" @change="changeAvatar($event)">
        <label for="file" class="button">更换头像</label>
        <span class="desc">支持jpg、png、bmp格式的图片，且文件小于20M</span>
      </div>
      <!-- 个人简介 -->
      <div class="describe">
        <label for="describe">个人简介</label>
        <!-- 设置不可拖动：resize:none -->
        <textarea id="describe" placeholder="用简短的话语描述一下自己吧" :value="describe"></textarea>
      </div>
      <!-- 保存修改 -->
      <div class="save">
        <input type="submit" value="保存修改">
      </div>
    </form>
  </div>
</template>

<script>
  export default {
    name: 'Edit',
    data() {
      return {
        username: 'minus',
        avatarUrl: '',
        describe: '简介'
      }
    },
    methods: {
      // 使上传的图片显示在页面上，供用户预览
      changeAvatar(e) {
        let file = e.target.files[0]
        let reader = new FileReader()
        let that = this
        reader.readAsDataURL(file)
        reader.onload = function() {
          that.avatarUrl = this.result
        }
      }
    }
  }
</script>

<style scoped>
  h2 {
    color: #86a3b1;
    margin-bottom: 40px;
  }

  div {
    display: flex;
    flex-direction: column;
  }

  label {
    letter-spacing: 2px;
    font-size: 16px;
  }

  input {
    width: 300px;
    height: 40px;
    line-height: 30px;
    border: 1px solid #E0E0FF;
    border-radius: 5px;
    font-size: 15px;
    margin: 10px 0;
    padding: 0 10px;
    box-shadow: 1px 1px 5px #E0E0FF;
  }

  .avatar {
    position: relative;
    margin-left: 450px;
  }

  input[type=file] {
    width: 100px;
    opacity: 0;
  }

  .avatar img {
    width: 200px;
    height: 200px;
  }

  .avatar .imgBox {
    position: absolute;
    top: -75px;
    left: 60px;
    overflow: hidden;
    width: 200px;
    height: 200px;
    border: 2px solid #86a3b1;
    border-radius: 10px;
  }

  .avatar .button {
    position: absolute;
    top: 170px;
    left: 85px;
    display: inline-block;
    width: 150px;
    height: 40px;
    line-height: 36px;
    text-align: center;
    letter-spacing: 2px;
    border: 2px solid #86a3b1;
    border-radius: 30px;
    background-color: #86a3b1;
    color: #fff;
    font-size: 18px;
    font-weight: 700;
    cursor: pointer;
    box-shadow: 1px 1px 5px #86a3b1;
  }

  .avatar .button:hover {
    background-color: transparent;
    color: #86a3b1;
  }

  .avatar .desc {
    position: absolute;
    top: 140px;
    left: 10px;
    color: #9CA3AF;
    font-size: 14px;
  }

  .describe {
    margin-top: -40px;
  }

  textarea {
    width: 300px;
    height: 150px;
    resize: none;
    border: 1px solid #E0E0FF;
    border-radius: 5px;
    font-size: 15px;
    margin: 10px 0;
    padding: 10px;
    box-shadow: 1px 1px 5px #E0E0FF;
  }

  .save {
    margin-top: 50px;
    margin-left: 320px;
  }

  input[type=submit] {
    width: 200px;
    height: 50px;
    line-height: 28px;
    outline: none;
    border: 0;
    border: 2px solid #86a3b1;
    border-radius: 30px;
    background-color: #86a3b1;
    color: #fff;
    font-size: 20px;
    font-weight: 700;
    letter-spacing: 2px;
    box-shadow: 1px 1px 5px #86a3b1;
    cursor: pointer;
  }

  input[type=submit]:hover {
    background-color: transparent;
    color: #86a3b1;
  }

  /* input的placeholder样式 */
  ::-webkit-input-placeholder { /* WebKit browsers */
    color: #9CA3AF;
    font-size: 13px;
  }

  ::-moz-placeholder { /* Mozilla Firefox 19+ */
    color: #9CA3AF;
    font-size: 13px;
  }

  :-ms-input-placeholder { /* Internet Explorer 10+ */
    color: #9CA3AF;
    font-size: 13px;
  }
</style>